package com.youmeek.ssm.module.category.service.impl;

import com.youmeek.ssm.module.category.mapper.ITaxCategoryMapper;
import com.youmeek.ssm.module.category.model.TaxCategory;
import com.youmeek.ssm.module.category.model.TaxCode;
import com.youmeek.ssm.module.common.model.TreeNode;
import com.youmeek.ssm.module.category.service.ITaxCodeService;
import com.youmeek.ssm.module.category.vo.TaxCategoryVO;
import com.youmeek.ssm.utils.ApplicationContextUtil;
import com.youmeek.ssm.utils.NodeUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class TaxCodeServiceImpl implements ITaxCodeService {

    @Resource
    private ITaxCategoryMapper taxCategoryMapper;

    private TaxCode getTopTaxCode() {
        return TaxCode.topCode();
    }

    private ITaxCodeService getProxy() {
        return ApplicationContextUtil.getBeanByClass(ITaxCodeService.class);
    }

    private List<TaxCode> getAllTaxCodeByProxy() {
        return getProxy().getAllTaxCode();
    }

    public TreeNode<TaxCode> getTopNodeByProxy() {
        return getProxy().getTopNode();
    }

    @Override
    public List<TaxCode> getAllTaxCode() {
        return taxCategoryMapper.selectAllTaxCode();
    }

    @Override
    public TreeNode<TaxCode> getTopNode() {
        TaxCode topCode = getTopTaxCode();
        return NodeUtils.buildNode(new TreeNode<TaxCode>(topCode), getAllTaxCodeByProxy(), new TaxCodeNodeHierachyImpl());
    }

    @Override
    public TreeNode<TaxCode> getTopNodeFirstLevel() {
        TreeNode<TaxCode> treeNode = new TreeNode<>();
        treeNode.setValue(TaxCode.topCode());
        List<TaxCode> taxCodeList = getChildsByMergeCode("0");
        TreeSet<TreeNode<TaxCode>> treeNodes = new TreeSet<>();
        for (TaxCode taxCode : taxCodeList) {
            treeNodes.add(new TreeNode<>(taxCode));
        }
        treeNode.setNodes(treeNodes);
        return treeNode;
    }

    @Override
    public TreeNode<TaxCode> getSpecialNode(String mergeCoding) {
        TaxCode specialTaxCode = new TaxCode();
        specialTaxCode.setMergeCoding(mergeCoding);
        TreeNode<TaxCode> treeNode = NodeUtils.buildNode(new TreeNode<>(specialTaxCode), getAllTaxCodeByProxy(),
                new TaxCodeNodeHierachyImpl());
        return treeNode;
    }

    @Override
    public TaxCategoryVO getTaxCategoryDetailByMergeCoding(String mergeCoding) {
        return null;
    }

    @Override
    public List<TaxCategory> selectByIds(List<String> ids) {
        return null;
    }

    @Override
    public List<TaxCategory> getProgenyLeafs(String mergeCoding) {
        return null;
    }

    @Override
    public List<TaxCode> getLeafTaxCodes(String mergeCoding) {
        return null;
    }

    @Override
    public void initTaxCategoryTree2Cache() {

    }

    @Override
    public Map<String, TaxCategory> getAllTaxCategory() {
        return null;
    }

    @Override
    public List<TaxCode> getChildsByMergeCode(String mergeCoding) {
        List<TaxCode> result = new ArrayList<>();
        TreeNode<TaxCode> topNode = getTopNodeByProxy();
        TreeNode<TaxCode> specialNode = getSpecialNode(mergeCoding, topNode);
        if (specialNode != null) {
            Collection<TreeNode<TaxCode>> nodes = specialNode.getNodes();
            for (TreeNode<TaxCode> node : nodes) {
                result.add(node.getValue());
            }
        }
        return result;
    }

    private TreeNode<TaxCode> getSpecialNode(String mergeCoding, TreeNode<TaxCode> topNode) {
        TaxCode taxCode = topNode.getValue();
        String pMergeCoding = taxCode.getMergeCoding();
        String pMergeCode = pMergeCoding.replaceAll("(00)+$", "");
        String mergeCode = mergeCoding.replaceAll("(00)+$", "");
        if (pMergeCode.equals(mergeCode)) {
            return topNode;
        } else {
            // 判断是对应节点下面的子节点，根节点比较特殊
            if (pMergeCode.equals("0") || mergeCode.startsWith(pMergeCode)) {
                return getNodeFromChilds(mergeCoding, topNode);
            } else {
                return null;
            }
        }
    }

    private TreeNode<TaxCode> getNodeFromChilds(String mergeCoding, TreeNode<TaxCode> topNode) {
        Collection<TreeNode<TaxCode>> childNodes = topNode.getNodes();
        if (childNodes.isEmpty()) {
            return null;
        }
        // 遍历下面的子节点
        for (TreeNode<TaxCode> treeNode : childNodes) {
            TreeNode<TaxCode> specialNode = getSpecialNode(mergeCoding, treeNode);
            if (specialNode == null) {
                continue;
            } else {
                return specialNode;
            }
        }
        return null;
    }
}
